package com.app.mymusic.ui;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.app.mymusic.R;
import com.app.mymusic.databinding.ActivityDatabindingBinding;
import com.app.mymusic.model.NetMusic;
import com.app.mymusic.model.User;

import java.util.ArrayList;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class TestActivity extends AppCompatActivity {

    private ArrayList<NetMusic.MusicInfo> mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityDatabindingBinding binding= DataBindingUtil.setContentView(this, R.layout.activity_databinding);
        User user=new User();
        user.setName("liunan");
        user.setAge(10);
        binding.setUser(user);
        binding.setToastClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TestActivity.this, "0000", Toast.LENGTH_SHORT).show();
            }
        });
    }


    private void searchMusic(String keyword) {
        Retrofit retrofit=new Retrofit.Builder().baseUrl("http://apis.baidu.com/geekery/music/")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        SearchOnNetFragment.interSearch iSearch=retrofit.create(SearchOnNetFragment.interSearch.class);
        iSearch.get(keyword,"10","1").subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<NetMusic>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
//                        Snackbar.make(mRecyclerView,"错误："+e,Snackbar.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(NetMusic mInfo) {
                        int code= mInfo.getCode();
                        if(code==0)
                        {
                            mDatas.addAll(mInfo.getData().getData());

                        }

                    }
                });
    }

}
